package com.hsck.order.kds.setup.fragment

import android.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.recyclerview.widget.GridLayoutManager
import com.hsck.order.kds.R
import com.hsck.order.kds.base.isnoam.view.BaseFragment
import com.hsck.order.kds.databinding.FragmentSbglBinding
import com.hsck.order.kds.setup.SetupActivity
import com.hsck.order.kds.setup.adapter.SbglAdapter
import kotlinx.android.synthetic.main.dialog_tishi.view.*

/**
 * 设备管理
 */
class SbglFragment : BaseFragment<FragmentSbglBinding>(), SetupActivity.UpdatableFragment {


    private val adapter by lazy { SbglAdapter(R.layout.item_sbgl) }

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentSbglBinding {
        return FragmentSbglBinding.inflate(inflater)
    }

    override fun loadDataOnce() {
        super.loadDataOnce()
        binding.apply {
            rvSbgl.layoutManager = GridLayoutManager(requireActivity(), 2)
            rvSbgl.adapter = adapter

            adapter.datas = listOf("", "", "", "", "", "", "")

        }

        adapter.onQingKongClick = {
            setQkDialog(it)
        }
        adapter.onSetZhuKdsClick = {

        }
        adapter.onJieBangClick = {
            setJieBangDialog(it)
        }
    }

    override fun onFragmentVisible() {
        //可见
        if (_binding != null) {

        }
    }

    override fun onFragmentInvisible() {
        //不可见

    }


    /**
     * 是否清空KSD履约数据？
     */
    private fun setQkDialog(data: String) {
        //R.style.***一定要写，不然不能充满整个屏宽，引用R.style.AppTheme就可以tm
        val createDialog = AlertDialog.Builder(activity, R.style.Dialog_FullScreen).create()
        // 设置Dialog的Window的软键盘模式为隐藏
        createDialog.getWindow()!!
            .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        val view = View.inflate(activity, R.layout.dialog_tishi, null)
        val window = createDialog.getWindow()

//        window.setGravity(Gravity.BOTTOM or Gravity.RIGHT)
        //设置dialog弹出后会点击屏幕或物理返回键，dialog不消失
        createDialog.setCanceledOnTouchOutside(false)
        createDialog.setCancelable(false)
        createDialog.show()
        window!!.setContentView(view)
        window.setBackgroundDrawableResource(R.drawable.shape_grayf5_borderless20)
        //获得window窗口的属性
        val params = window.getAttributes()
        //设置窗口宽度为充满全屏
        val width = WindowManager.LayoutParams.WRAP_CONTENT
        val height = WindowManager.LayoutParams.WRAP_CONTENT
        params.width = width
        params.height = height
//        params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE//显示dialog的时候,就显示软键盘
        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND//就是这个属性导致window后所有的东西都成暗淡
        params.dimAmount = 0.5f//设置对话框的透明程度背景(非布局的透明度)
        //将设置好的属性set回去
        window.setAttributes(params)

        view.tv_dialog_tishi_title.text = "操作确认"
        view.tv_dialog_tishi_msg.text = "是否清空KSD履约数据？"
        view.btn_dialog_tishi_qx.text = "取消"
        view.btn_dialog_tishi_qr.text = "清空"
        view.btn_dialog_tishi_qx.setOnClickListener {
            createDialog.dismiss()
        }
        view.btn_dialog_tishi_qr.setOnClickListener {
            createDialog.dismiss()
        }
    }

    /**
     * 是否解绑设备与站点绑定关系？
     */
    private fun setJieBangDialog(data: String) {
        //R.style.***一定要写，不然不能充满整个屏宽，引用R.style.AppTheme就可以tm
        val createDialog = AlertDialog.Builder(activity, R.style.Dialog_FullScreen).create()
        // 设置Dialog的Window的软键盘模式为隐藏
        createDialog.getWindow()!!
            .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        val view = View.inflate(activity, R.layout.dialog_tishi, null)
        val window = createDialog.getWindow()

//        window.setGravity(Gravity.BOTTOM or Gravity.RIGHT)
        //设置dialog弹出后会点击屏幕或物理返回键，dialog不消失
        createDialog.setCanceledOnTouchOutside(false)
        createDialog.setCancelable(false)
        createDialog.show()
        window!!.setContentView(view)
        window.setBackgroundDrawableResource(R.drawable.shape_grayf5_borderless20)
        //获得window窗口的属性
        val params = window.getAttributes()
        //设置窗口宽度为充满全屏
        val width = WindowManager.LayoutParams.WRAP_CONTENT
        val height = WindowManager.LayoutParams.WRAP_CONTENT
        params.width = width
        params.height = height
//        params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE//显示dialog的时候,就显示软键盘
        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND//就是这个属性导致window后所有的东西都成暗淡
        params.dimAmount = 0.5f//设置对话框的透明程度背景(非布局的透明度)
        //将设置好的属性set回去
        window.setAttributes(params)

        view.tv_dialog_tishi_title.text = "操作确认"
        view.tv_dialog_tishi_msg.text = "是否清空KSD履约数据？"
        view.btn_dialog_tishi_qx.text = "取消"
        view.btn_dialog_tishi_qr.text = "解绑"
        view.btn_dialog_tishi_qx.setOnClickListener {
            createDialog.dismiss()
        }
        view.btn_dialog_tishi_qr.setOnClickListener {
            createDialog.dismiss()
        }
    }

    /**
     * 设为主KDS
     */
    private fun setZhuKdsDialog(data: String) {
        //R.style.***一定要写，不然不能充满整个屏宽，引用R.style.AppTheme就可以tm
        val createDialog = AlertDialog.Builder(activity, R.style.Dialog_FullScreen).create()
        // 设置Dialog的Window的软键盘模式为隐藏
        createDialog.getWindow()!!
            .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        val view = View.inflate(activity, R.layout.dialog_tishi, null)
        val window = createDialog.getWindow()

//        window.setGravity(Gravity.BOTTOM or Gravity.RIGHT)
        //设置dialog弹出后会点击屏幕或物理返回键，dialog不消失
        createDialog.setCanceledOnTouchOutside(false)
        createDialog.setCancelable(false)
        createDialog.show()
        window!!.setContentView(view)
        window.setBackgroundDrawableResource(R.drawable.shape_grayf5_borderless20)
        //获得window窗口的属性
        val params = window.getAttributes()
        //设置窗口宽度为充满全屏
        val width = WindowManager.LayoutParams.WRAP_CONTENT
        val height = WindowManager.LayoutParams.WRAP_CONTENT
        params.width = width
        params.height = height
//        params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE//显示dialog的时候,就显示软键盘
        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND//就是这个属性导致window后所有的东西都成暗淡
        params.dimAmount = 0.5f//设置对话框的透明程度背景(非布局的透明度)
        //将设置好的属性set回去
        window.setAttributes(params)

        view.tv_dialog_tishi_title.text = "操作确认"
        view.tv_dialog_tishi_msg.text = "是否设为主KDS？"
        view.btn_dialog_tishi_qx.text = "取消"
        view.btn_dialog_tishi_qr.text = "确认"
        view.btn_dialog_tishi_qx.setOnClickListener {
            createDialog.dismiss()
        }
        view.btn_dialog_tishi_qr.setOnClickListener {
            createDialog.dismiss()
        }
    }


}